<?php

// No direct access to this file
defined('_JEXEC') or die('Restricted access');
JTable::addIncludePath(JPATH_COMPONENT.DIRECTORY_SEPARATOR.'tables');

class KetnoiModelHopdong extends JModelLegacy
{
	public function getTable($type = 'Hopdong', $prefix = 'KetnoiTable', $config = array())
	{
		return JTable::getInstance($type, $prefix, $config);
	}
	
	function loadGridData($limitstart,$limit,$where)
    {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select("i.serial,h.*,p.ma as ma_machine,p.ten as ten_machine,ht.ten as hinhthucmay,hs.ten as hopdongstatus");
        $query->select("case when v.ma is null then '' else v.ma end as manhacungcap,case when v.ten is null then '' else v.ten end as tennhacungcap");
        $query->select("c.ma as makhachhang,c.ten as tenkhachhang");
        $query->from("#__hopdong h")
        	  ->innerJoin("#__inventory i on h.inventory_id = i.id")
       		  ->innerJoin("#__products p on p.id = i.machine_id")	
       		  ->innerJoin("#__hinhthuc ht on ht.id = h.hinhthuc")
       		  ->innerJoin("#__customer c on c.id = h.customer_id")
       		  ->innerJoin("#__hopdong_status hs on hs.id = h.status")
       		  ->leftJoin("#__vendor v on v.id = i.vendor_id");
        $query->where($where);
        $query .= " limit $limitstart,$limit";  
        $db->setQuery($query);
        $result = $db->loadObjectList();
        return $result; 
    }
    
	function loadAllGridData($where)
    {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select("count(*)");
        $query->from("#__hopdong h")
        	  ->innerJoin("#__inventory i on h.inventory_id = i.id")
       		  ->innerJoin("#__products p on p.id = i.machine_id")	
       		  ->leftJoin("#__vendor v on v.id = i.vendor_id");
        $query->where($where); 
        $db->setQuery($query);
        $result = $db->loadResult();
        return $result; 
    }
    
	function save()
    {
    	global $app;
    	$result = array("success"=>true,"error"=>"");
    	$table = $this->getTable();
    	$post = JRequest::get("post");
    	date_default_timezone_set("Asia/Ho_Chi_Minh");
    	$post["datecreate"] = date("Y-m-d");

		$table = Utility::save($table, $post);
		$result["id"] = $table->id;
		
		//update serial status
    	if($post["status"] == "dathanhly" || $post["status"] == "dangtheodoi")
		{
			$this->updateInventoryStatus(0, $post["inventory_id"]);
		}else 
		{
			$this->updateInventoryStatus(1, $post["inventory_id"]);
		}
    	return $result;
    }
    
    function updateInventoryStatus($status,$inventory)
    {
    	$db = JFactory::getDbo();
		$query = "update #__inventory set status = $status where id = '$inventory'";
		$db->setQuery($query);
		$db->query();
    }
    
    function loadData($id)
    {
    	$table = $this->getTable();
    	$table->load($id);
    	
    	$db = JFactory::getDbo();
		$query = $db->getQuery(true);
    	$query->select("i.serial,h.*,p.ma as ma_machine,p.ten as ten_machine");
        $query->select("v.ma as ma_nhacungcap,v.ten as tennhacungcap");
        $query->from("#__hopdong h")
        	  ->innerJoin("#__inventory i on h.inventory_id = i.id")
       		  ->innerJoin("#__products p on p.id = i.machine_id")	       		  
       		  ->leftJoin("#__vendor v on v.id = i.vendor_id");
       	$query->where("i.id = '".$table->inventory_id."' and h.id = $id");	  
		$db->setQuery($query);
    	return $db->loadObject();
    }
    
	function checkSerial()
    {
    	$serial = JRequest::getVar("serial");
    	$id = JRequest::getVar("id");
    	$db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select("count(*)");
        /*$query->from("#__inventory");
        $query->where("status = 1 and serial = '$serial'");*/
        $query->from("#__hopdong");
        $query->where("(status = 'dachothue' or status = 'daban') and inventory_id = $serial and id <> $id");
        $db->setQuery($query);
        $result = $db->loadResult();
        return $result > 0;
    }
    
    /*
	function delete()
	{
		$id = JRequest::getVar("id");
		$db = JFactory::getDbo();
		$query = "update #__vendor set is_delete = 1 where id = $id";
		$db->setQuery($query);
		$db->query();

	}*/
	
	function loadAllCustomer()
	{
		$db = JFactory::getDbo();
		$query = "select * from #__customer where is_delete = 0";
		$db->setQuery($query);
		return $db->loadObjectList();
	}
	
	function loadAllHinhthucmay()
	{
		$db = JFactory::getDbo();
		$query = "select * from #__hinhthuc order by ordering ASC";
		$db->setQuery($query);
		return $db->loadObjectList();
	}
	
	function loadAllHopdongstatus()
	{
		$db = JFactory::getDbo();
		$query = "select * from #__hopdong_status";
		$db->setQuery($query);
		return $db->loadObjectList();
	}
	
	function loadAllSerial()
	{
		$db = JFactory::getDbo();
		$query = $db->getQuery(true);
		$query->select("i.*,p.ma as ma_machine,p.ten as ten_machine");
		$query->select("p.sothangbaohanh,p.sodungluongbaohanh");
        $query->select("v.ma as ma_nhacungcap,v.ten as tennhacungcap");
        $query->from("#__inventory i")
       		  ->innerJoin("#__products p on p.id = i.machine_id")	
       		  ->leftJoin("#__vendor v on v.id = i.vendor_id");
       	$query->where("i.is_delete = 0 and i.status = 0");	  
       	$db->setQuery($query);
		return $db->loadObjectList();	  
	}
}
